package red.internal.client.registry;

import red.client.registry.Host;

import java.util.Objects;

/**
 * @author Jin Zheng
 * @since 2020-07-12
 */
public class DefaultHost implements Host
{
	private final String ip;
	private final int port;
	private String token;

	public DefaultHost(String ip, int port)
	{
		this.ip = ip;
		this.port = port;
	}

	public DefaultHost(String addr)
	{
		String[] str = addr.split(SPLIT);
		this.ip = str[0];
		this.port = Integer.parseInt(str[1]);
	}

	@Override
	public boolean equals(Object o)
	{
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		DefaultHost that = (DefaultHost) o;
		return port == that.port && Objects.equals(ip, that.ip);
	}

	@Override
	public int hashCode()
	{
		return Objects.hash(ip, port);
	}

	@Override
	public String toString()
	{
		return String.format("%s:%d", ip, port);
	}

	@Override
	public String getIp()
	{
		return ip;
	}

	@Override
	public int getPort()
	{
		return port;
	}

	public String getToken()
	{
		return token;
	}

	public void setToken(String token)
	{
		this.token = token;
	}
}
